export function transformQueryToStr(query) {
    // tslint:disable-next-line:max-line-length
    return Object.keys(query).filter(item => query[item] !== undefined).reduce((total, item) => `${total} ${total ? ' and ' : ''}${item}:${query[item]}`, '');
}

export function addSelectList(str, line, offset) {
    return `${str} | select * limit ${line}, ${offset}`;
}

export function addSelectCount(str) {
    return `${str} | select count(*) as totalCount`
}

export function getEventNumAndUserNumQueryStr(query) {
    return `${transformQueryToStr(query)} | select count(*) as eventNum, approx_distinct(uid) as userNum`;
}

export function getTimeQueryStr(query, flag) {
    // tslint:disable-next-line:max-line-length
    return `${transformQueryToStr(query)} | select date_trunc('${flag}', time) as time , count(*) as eventNum, approx_distinct(uid) as userNum group by time order by time limit 1000`
}

export function getTimeQueryByErrorStr(query, flag) {
    // tslint:disable-next-line:max-line-length
    return `${transformQueryToStr(query)} | select date_trunc('${flag}', time) as time , approx_distinct(errorId) as errorNum group by time order by time limit 1000`
}

export function getTimeQueryByLogStr(query, flag) {
    return `${transformQueryToStr(query)} | select date_trunc('${flag}', time) as time, count(*) as logNum group by time order by time limit 1000`
}

export function getCategoryNumByLogStr(query, flag) {
    // tslint:disable-next-line:max-line-length
    return `${transformQueryToStr(query)} |  select "${flag}", count, count *1.0/sum(count) over() as percentage from( select count(1) as count , "${flag}" from (select "${flag}" from log limit 100000) group by "${flag}" order by count desc) order by count desc limit 100`
}